<template>

    <div class="InventoryManage-page-content">
        <div class="InventoryManage-page-form">
            <el-input v-model="formData.orderNo" style="width:300px" placeholder="订单号/药品" :suffix-icon="Search" />
            <!-- <el-select v-model="formData.storageId" clearable style="width:300px;margin-left: 20px;" placeholder="药库"   >
                <el-option v-for="item in storageData" :key="item.storageId" :label="item.storageName" :value="item.storageId" />
            </el-select>   -->
            <el-select v-model="formData.orderStatus" clearable filterable style="width:300px;margin-left: 20px;"  placeholder="订单状态"   >
                <el-option v-for="item in reqOrderStatusEnum" :key="item.value" :label="item.text" :value="item.value" />
            </el-select>  
            <el-select placeholder="科室" style="width: 280px;margin-left: 20px;" clearable filterable v-model="formData.deptId" class="shift-search-select">
                <el-option v-for="item in deptList" :key="item.deptId" :label="item.deptName" :value="item.deptId" />
            </el-select> 
            <el-button type="primary" @click="handlePageChange(1)" style="margin-left: 20px;">查询</el-button>
          
            <div class="InventoryManage-page-form-right">
                <el-button type="primary" v-btnPermission="'btn-req-add'" @click="add">新建</el-button>
            </div>
        </div>
        <div class="InventoryManage-page-table">
            <Table :columns="tableColumns" :loading="loading" :data-source="tableData" />

        </div>
        <div class="InventoryManage-page-pagination">
            <Pagination style="margin-left: auto;" :key="pageSize + 'Pagination' + currentPage" :count="total"
                :page-size="pageSize" :pageNo="currentPage" @onPagechange="handlePageChange"
                @onSizechange="handleSizeChange" />
        </div>
        <updata ref="updataRef" :data-type="dataType" @closeDrawer="getOrderReqListApi" />
        <reviewPurchase ref="reviewPurchaseRef"  @closeDrawer="getOrderReqListApi" />
        <!-- <refusePurchase ref="refusePurchaseRef"  @closeDrawer="getOrderReqListApi" />
        <settlePurchase ref="settlePurchaseRef"  @closeDrawer="getOrderReqListApi" /> -->

    </div>

</template>

<script setup lang="ts">
import { Search } from '@element-plus/icons-vue'
import type {  TableColumn } from '@/types/table';
import type {PurchaseInfo,PurchasePagination} from '@/types/purchase'
import {getOrderReqList} from '@/api/req'
import updata from './common/updata.vue'
import reviewPurchase from './common/reviewPurchase.vue'
import type { List as storageList } from '@/types/storage/index';
import stores from '@/store';
import { deptGetSelectList } from '@/api/account'
const storageInfo = defineProps<{ storageInfo: storageList }>();

// 订单状态PurchaseOrderStatusEnum
const reqOrderStatusEnum =  ref<any>(stores.useBaseDataStore().getEnumSelect('reqOrderStatusEnum'))

const updataRef = ref<InstanceType<typeof updata>>();
    //审核
const reviewPurchaseRef = ref<InstanceType<typeof reviewPurchase>>();
//     //退款
// const refusePurchaseRef = ref<InstanceType<typeof refusePurchase>>();
//     //结算
// const settlePurchaseRef = ref<InstanceType<typeof settlePurchase>>();

const dataType = ref<number>(0)
const loading = ref<boolean>(false)
const formData = ref<{
    deptId: any,
    orderStatus: any,
    orderNo:any,
    storageId: any
    pageNo: number,
    pageSize: number,
}>({
    deptId:'',
    orderNo:'',
    orderStatus:'',
    storageId:storageInfo.storageInfo.storageId,
    pageNo:1,
    pageSize:10,
})

const add = ()=>{
    dataType.value = 0
    updataRef.value?.open(0,storageInfo.storageInfo.storageId)
}
//科室
const deptList = ref<any[]>([]);
const getDeptGetSelectList = async () => {
  let res = await deptGetSelectList()
    if(res.code != '200'){
        //ElMessage.error(res.msg);
        return
    }
  deptList.value = res.data
}
getDeptGetSelectList();

const tableColumns: TableColumn[] = [
    {
        prop: 'orderNo',
        label: '单据编号',
        type: 'text',
        permissions:'btn-req-xq',
        width: 200,
        textColor:'color:#18B3AB',
        componentProps: {
            fixed: 'left',
        },
        buttonGroup: [
            {
                label: 'orderNo', type: 'text', click: (row: any) => {
                    dataType.value = 1
                    updataRef.value?.open1(row.orderId,storageInfo.storageInfo.storageId)
                }
            },
        ]
    },
    { prop: 'orderStatusText', label: '单据状态' },
    { prop: 'storageName', label: '领用药库' },
    { prop: 'reqUserName', label: '领用人员' },
    { prop: 'reqDeptName', label: '领用科室' },
    { prop: 'drugKindCount', label: '品种数量' },
    { prop: 'totalQty', label: '领用数量' },
    { prop: 'remark', label: '备注' },
    { prop: 'inUserName', label: '制单人员' },
    { prop: 'inTime', label: '制单时间' },
    {
        prop: 'action',
        label: '操作',
        type: 'action',
        width: 120,
        componentProps: {
            fixed: 'right',

        },
        buttonGroup: [
            {
                label: '详情', permissions:"btn-req-xq", type: 'text', click: (row: PurchaseInfo) => {
                    dataType.value = 1
                    updataRef.value?.open1(row.orderId,storageInfo.storageInfo.storageId)
                }
            },
            {
                label: '审核', type: 'text',permissions:'btn-rep-sh', statusName: ['orderStatus'], status: '1', click: (row: PurchaseInfo) => {
                    dataType.value = 1
                    reviewPurchaseRef.value?.open(row.orderId)
                }
            },
        ]
    }
]
const tableData = ref<PurchaseInfo[]>([])
const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(0);
const getOrderReqListApi = async()=>{
    loading.value = true
    formData.value.pageNo = currentPage.value
    formData.value.pageSize = pageSize.value
    let res = await getOrderReqList(formData.value) 
    if(res.code != '200'){
        //ElMessage.error(res.msg);
        return
    }
    res.data.list.forEach((item: any) => {
      item.isEnabled = item.isEnabled === '1' ? '启用' : '停用';
      for (let index = 0; index < reqOrderStatusEnum.value.length; index++) {
        let items = reqOrderStatusEnum.value[index];
        if(item.orderStatus == items.value){
            item.orderStatusText = items.text
        }
      }
      
      if(item.orderStatus == '1'){
        item.settleStatus = '0'
      }
      
    });
    loading.value = false

   tableData.value = res.data.list
   total.value = res.data.totalCount
}
getOrderReqListApi()




const handlePageChange = (page: number) => {
    currentPage.value = page;
    getOrderReqListApi()
};
const handleSizeChange = (size: number) => {
    currentPage.value = 1;

    pageSize.value = size;
    getOrderReqListApi()

};


</script>

<style lang="scss">
.InventoryManage {
    padding: 24px;
    height: 100%;
}

.InventoryManage-page {
    background: #fff;
    border-radius: 6px;
    height: 100%;

    .InventoryManage-page-nav {
        display: flex;
        align-items: center;
        justify-content: left;
        border-radius: 4px;
        border-bottom: 1px solid #E7E7E7;

        .el-dropdown-link {
            background: rgba(22, 173, 164, 0.08);
            border-radius: 0px 0px 0px 0px;
            padding: 16px 21px;
            cursor: pointer;
            color: var(--el-color-primary) !important;
            display: flex;
            font-size: 16px;
            align-items: center;
        }

    }

    .InventoryManage-page-content {
        padding: 21px 24px;
        height: calc(100% - 50px);

        .InventoryManage-page-form {
            display: flex;
            align-items: center;

            .InventoryManage-page-form-right {
                margin-left: auto;
            }
        }

        .InventoryManage-page-table {
            margin-top: 22px;
            height: calc(100vh - 300px);
        }

        .InventoryManage-page-pagination {
            margin-top: 24px;
            display: flex;
            align-items: center;

            .InventoryManage-page-pagination-total {
                margin-right: auto;
                font-family: Microsoft YaHei, Microsoft YaHei;
                font-weight: 400;
                font-size: 14px;
                color: rgba(0, 0, 0, 0.6);
                text-align: left;
                font-style: normal;
                text-transform: none;
            }
        }
    }

}
</style>